import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class HomeScreen extends StatefulWidget {
  final Widget child;
  
  const HomeScreen({super.key, required this.child});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;
  
  // 底部导航项
  final List<_NavigationItem> _navigationItems = [
    _NavigationItem(
      icon: Icons.home,
      label: '每日必看',
      path: '/',
    ),
    _NavigationItem(
      icon: Icons.school,
      label: '单词记忆',
      path: '/word-list',
    ),
    _NavigationItem(
      icon: Icons.quiz,
      label: '题目练习',
      path: '/practice',
    ),
    _NavigationItem(
      icon: Icons.person,
      label: '我的',
      path: '/profile',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: widget.child,
      bottomNavigationBar: NavigationBar(
        selectedIndex: _currentIndex,
        onDestinationSelected: (index) {
          setState(() {
            _currentIndex = index;
          });
          context.go(_navigationItems[index].path);
        },
        labelBehavior: NavigationDestinationLabelBehavior.alwaysShow,
        destinations: _navigationItems.map<NavigationDestination>((item) => NavigationDestination(
          icon: Icon(item.icon),
          label: item.label,
        )).toList(),
      ),
    );
  }
  
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _updateCurrentIndex();
  }
  
  // 根据当前路径更新底部导航栏选中项
  void _updateCurrentIndex() {
    final String location = GoRouterState.of(context).matchedLocation;
    
    if (location.startsWith('/profile')) {
      setState(() => _currentIndex = 3);
    } else if (location.startsWith('/practice')) {
      setState(() => _currentIndex = 2);
    } else if (location.startsWith('/word-list')) {
      setState(() => _currentIndex = 1);
    } else {
      setState(() => _currentIndex = 0);
    }
  }
}

// 导航项数据类
class _NavigationItem {
  final IconData icon;
  final String label;
  final String path;
  
  _NavigationItem({
    required this.icon,
    required this.label,
    required this.path,
  });
}